package com.app.service.Impl;

import com.app.mapper.StudyAdminMapper;
import com.app.mapper.StudyMapper;
import com.app.model.ActivityModel;
import com.app.model.AreaModel;
import com.app.model.StudyModel;
import com.app.model.SystemModel;
import com.app.service.ActivityService;
import com.app.service.AreaService;
import com.app.service.StudyAdminService;
import com.app.util.CommonHelp;
import com.app.vo.StudyVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudyAdminServiceImpl implements StudyAdminService {
    @Autowired
    private StudyAdminMapper studyAdminMapper;
    @Autowired
    private StudyMapper studyMapper;
    @Autowired
    private AreaService areaService;
    @Override
    public void insert(StudyModel study) {
        study.setCreateTime(CommonHelp.getNowTimestamp());
        studyAdminMapper.insert(study);
    }

    @Override
    public void updateById(StudyModel study) {
        study.setUpdateTime(CommonHelp.getNowTimestamp());
        studyAdminMapper.updateById(study);
    }

    @Override
    public void deleteStudyById(Integer id) {
        studyAdminMapper.deleteById(id);
    }

    @Override
    public StudyVo selectStudyList(Integer page, Integer limit, String title,Integer villageId) {
        if (page == null || limit == null) {
            return null;
        }
        page = (page - 1) * limit;
        List<StudyModel> studyModels = studyAdminMapper.selectStudyList(page, limit, title,villageId);
        for (int i = 0; i < studyModels.size(); i++) {
            if (studyModels.get(i).getVillageId() != 0) {
                AreaModel villageAreaModel = areaService.selectById(studyModels.get(i).getVillageId());
                AreaModel townAreaModel = areaService.selectById(villageAreaModel.getPid());
                AreaModel cityAreaModel = areaService.selectById(townAreaModel.getPid());
                studyModels.get(i).setVillageText(cityAreaModel.getName() + "/" + townAreaModel.getName() + "/" + villageAreaModel.getName());
            } else {
                studyModels.get(i).setVillageText("");
            }
        }
        List<SystemModel> systemModels = studyMapper.selectTypeList();
        for (int i = 0; i < studyModels.size(); i++) {
            for (int i1 = 0; i1 < systemModels.size(); i1++) {
                if (studyModels.get(i).getType().equals(systemModels.get(i1).getId())) {
                    studyModels.get(i).setTypeText(systemModels.get(i1).getName());
                }
            }
        }
        Integer count = null;
        QueryWrapper<StudyModel> queryWrapper = new QueryWrapper<>();
        if (villageId != null && villageId != 0) {
            queryWrapper.eq("village_id", villageId);
            count = studyAdminMapper.selectCount(queryWrapper);
        } else {
            count = studyAdminMapper.selectCount(null);
        }
        StudyVo vo = new StudyVo();
        vo.setList(studyModels);
        vo.setCount(count);
        return vo;
    }
}
